import { parsingJson } from "@/utils/parsing";
import tool from "@/utils/tool";
export const toTab = (item, data, router) => {
  let queryData;
  if (item.paramTemplate) {
    queryData = parsingJson(data, item);
  }
  if (item.fileName) {
    queryData.fileName = item.fileName;
  }
  //动态添加路由
  let menu = tool.data.get("MENU");
  let exist = false;
  const name = item.path ?  item.path.replace(/\//g, ''): item.url.split("/").map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1)).join("");
  if (router.hasRoute(name)) { exist = true }
  if (exist == false) {
    let route = {
      component: item.url.slice(1),
      name: name,
      path: item.path ? item.path : item.url,
      meta: {
        affix: false,
        fullpage: false,
        hidden: true,
        icon: "el-icon-add-location",
        title: item.title,
        type: "menu",
      },
      children:null,
      redirect: undefined
    };
    menu[menu.length - 1].children.push(route);
    tool.data.set("MENU", menu);
    route.component = loadComponent(route.component);
    route.meta.breadcrumb = []
    route.meta.breadcrumb.push(route)
    router.addRoute('layout', route);
    router.push({
      path: item.path ? item.path : item.url,
      query: queryData,
    });
  } else {
    router.push({
      path: item.path ? item.path : item.url,
      query: queryData,
    });
  }
};
function loadComponent(component) {
  if (component) {
    let cpn = () => import(/* webpackChunkName: "[request]" */ `@/views/${component}`)
    return cpn
  }
  else {
    return () => import(`@/layout/other/empty`)
  }

}